package com.jinbiao.quasar.controller;

import cn.hutool.core.util.StrUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.constant.FileDir;
import com.jinbiao.quasar.service.FileService;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.UploadedFile;

@Controller
@OperationLog
public class UploaderController {
    @Inject("fileService")
    FileService fileService;
    @Mapping("/upload/upload-avatar")
    public AjaxJson uploadAvatar(UploadedFile file) {
        String url = fileService.saveFile(file, FileDir.AVATAR);
        if (StrUtil.isNotEmpty(url))
            return AjaxJson.getSuccessData(url);
        else
            return AjaxJson.getError("上传失败");
    }
    @Mapping("/upload/upload-banner-image")
    public AjaxJson uploadBannerImage(UploadedFile file) {
        String url = fileService.saveFile(file, FileDir.BANNER);
        if (StrUtil.isNotEmpty(url))
            return AjaxJson.getSuccessData(url);
        else
            return AjaxJson.getError("上传失败");
    }
    @Mapping("/upload/upload-logo")
    public AjaxJson uploadLogo(UploadedFile file) {
        String url = fileService.saveFile(file, FileDir.LOGO);
        if (StrUtil.isNotEmpty(url))
            return AjaxJson.getSuccessData(url);
        else
            return AjaxJson.getError("上传失败");
    }
    @Mapping("/upload/upload-favicon")
    public AjaxJson uploadFavicon(UploadedFile file) {
        String url = fileService.saveFile(file, FileDir.FAVICON);
        if (StrUtil.isNotEmpty(url))
            return AjaxJson.getSuccessData(url);
        else
            return AjaxJson.getError("上传失败");
    }
}
